/**
 *
 * @param array
 * @param fromIndex
 * @param toIndex
 * @return {array}
 * @examples
 *
 *  const array = [1,2,3,4,5,6,7]
 *  swap(array, 2, 5)
 *  swap(array, -1, 5)
 *  swap([...array], 2, 5)
 */
export function swap(array: any[], fromIndex: number, toIndex: number): any[] {
  if (array == null) return array
  fromIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex
  toIndex = toIndex < 0 ? array.length + toIndex : toIndex
  if ((fromIndex >= 0 && fromIndex < array.length) && (toIndex >= 0 && toIndex < array.length)) {
    const tmp = array[fromIndex]
    array[fromIndex] = array[toIndex]
    array[toIndex] = tmp
  }

  return array
}
